package com.irdstudio.apicenter.console.controller.appframe;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.dm.AppFrameStyle;
import com.irdstudio.apicenter.console.service.appframe.AppFrameStyleService;

import javax.servlet.http.HttpServletRequest;

/**
 * 平台模块管理
 * 
 * @author jr
 * 
 */
@Controller
@RequestMapping(value = "/appFrameStyle")
public class AppFrameStyleAction {
	@Autowired
	private AppFrameStyleService appFrameStyleService;

	@RequestMapping(value = "appFrameStyleDetail1")
	public ModelAndView queryWithKeys(HttpServletRequest request)
			throws Exception {
		String appid = request.getParameter("appid");
		ModelAndView modelAndView = new ModelAndView();
		AppFrameStyle appFrameStyle = appFrameStyleService
				.queryAppFrameStyleDetail(appid);
		appFrameStyle.setAppId(appid);
		modelAndView.addObject("appFrameStyle", appFrameStyle);
		modelAndView.setViewName("appframe/style");
		return modelAndView;
	}

	@RequestMapping(value = "updateAppFrameStyle")
	public void updateAppFrameStart(
			@ModelAttribute("appFrameStyle") AppFrameStyle appFrameStyle,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView modelAndView = new ModelAndView();

		int flag = -1;
		AppFrameStyle appFrameStyle1 = appFrameStyleService
				.queryAppFrameStyleDetail(appFrameStyle.getAppId());
		if (appFrameStyle1.getAppId() == null
				|| "".endsWith(appFrameStyle1.getAppId())) {
			flag = appFrameStyleService.insertOne(appFrameStyle);
		} else {
			flag = appFrameStyleService.updateByKeys(appFrameStyle);
		}
		// int flag = appFrameStyleService.updateByKeys(appFrameStyle);
		/*
		 * modelAndView.addObject("appFrameStyle", appFrameStyle);
		 * modelAndView.addObject("result",flag);
		 * modelAndView.setViewName("appframe/style"); return modelAndView;
		 */
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}

	}

}
